from sklearn.datasets import load_iris, load_breast_cancer, load_wine
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB, MultinomialNB, BernoulliNB
from sklearn.metrics import accuracy_score, confusion_matrix

# 加载数据集
iris = load_iris()
breast_cancer = load_breast_cancer()
wine = load_wine()

# 定义朴素贝叶斯算法
naive_bayes_algorithms = {
    "高斯朴素贝叶斯": GaussianNB(),
    "多项式贝叶斯": MultinomialNB(),
    "伯努利贝叶斯": BernoulliNB()
}

datasets = {
    "iris": iris,
    "breast_cancer": breast_cancer,
    "wine": wine
}

# 对每个数据集使用每种算法进行训练和评估
for dataset_name, dataset in datasets.items():
    X_train, X_test, y_train, y_test = train_test_split(dataset.data, dataset.target, test_size=0.2, random_state=42)
    print(f"数据集: {dataset_name}")
    for algorithm_name, algorithm in naive_bayes_algorithms.items():
        algorithm.fit(X_train, y_train)
        y_pred = algorithm.predict(X_test)
        accuracy = accuracy_score(y_test, y_pred)
        confusion_mat = confusion_matrix(y_test, y_pred)
        print(f"算法: {algorithm_name}")
        print(f"准确率: {accuracy}")
        print("混淆矩阵:")
        print(confusion_mat)
        print()
